package com.ye.zcswzlms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ye.zcswzlms.entity.Lostproperty;
import com.ye.zcswzlms.entity.TbUr;
import com.ye.zcswzlms.entity.Users;
import com.ye.zcswzlms.mapper.TbUrMapper;
import com.ye.zcswzlms.mapper.UsersMapper;
import com.ye.zcswzlms.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 叶德辉
 * @since 2020-12-08
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    TbUrMapper tbUrMapper;
    public String register(Users user){
        int flag=checkReRegister(user.getStuNo());
        if (flag==1){
            return "该用户已注册";
        }else if(flag==0){
            UpdateWrapper<Users> uw=new UpdateWrapper();
            uw.eq("stuNo",user.getStuNo())
              .set("login",1);
            if (usersMapper.update(user,uw)>0){
                return "注册成功";
            }
        }
        user.setLogin(1);
        if (usersMapper.insert(user)>0){
            TbUr tbUr = new TbUr();
            tbUr.setTbUrRole(2);
            tbUr.setTbUrUser(Integer.parseInt(user.getStuNo()));
            tbUrMapper.insert(tbUr);
            return "注册成功";
        }else {
            return "注册失败";
        }
    }
    public int checkReRegister(String stuNo){
        QueryWrapper<Users> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("stuNo",stuNo);
        queryWrapper.eq("login",1);
        if (usersMapper.selectOne(queryWrapper)!=null){
            return 1;
        }
        QueryWrapper<Users> queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("stuNo",stuNo);
        queryWrapper1.eq("login",0);
        if (usersMapper.selectOne(queryWrapper1)!=null){
            return 0;
        }
        return -1;
    }
    public boolean userInsert(String stuNo,String name,String tel){
        Users users = new Users();
        users.setName(name);
        users.setStuNo(stuNo);
        users.setTel(tel);
        if (usersMapper.insert(users)>0){
            return true;
        }
        return false;
    }
    public void checkLogin(String username,String password) throws Exception{
        Subject subject=SecurityUtils.getSubject();
        UsernamePasswordToken Token=new UsernamePasswordToken(username,password);
        subject.login(Token);
    }

    public void addAdmin(String stuNo){
         usersMapper.addUser(stuNo);
    }

    public List<Users> queryUsers(int page){
        IPage<Users> UsersPage = new Page<>(page, 4);//参数一是当前页，参数二是每页个数
        QueryWrapper qw =new QueryWrapper();
        qw.orderByAsc("grade");
        UsersPage = usersMapper.selectPage(UsersPage, qw);
        return UsersPage.getRecords();
    }
    public List<Users> queryByName(String name){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("name",name);
        List<Users> list = usersMapper.selectList(qw);
        return list;
    }

}
